//global variable
var xmlHttp;

//function to create XmlHttpObject
function CreateXmlHttpObject(){ 
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   return null;
}

function showHome(sesison){
  
  xmlHttp=CreateXmlHttpObject();
  if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request");
  	return;
  } 
  var url="http://localhost:8080/DreamScope/HomePageWriter";
  xmlHttp.onreadystatechange=pasteHome ;
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xmlHttp.send("sessionId="+session);
}

//Function to perform something when XmlHttpObject state changes
function pasteHome(){ 
  if ((xmlHttp.readyState==4 || xmlHttp.readyState=="complete") && xmlHttp.status ==200){   
	  document.getElementById("main").innerHTML=xmlHttp.responseText;
  } 
} 

function showJavaexplored(session){
  xmlHttp=CreateXmlHttpObject();
  var str = "sample";
  if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request");
  	return;
  } 
  var url="pages/javaexplored/javaexploredHome.jsp";
  xmlHttp.onreadystatechange=pasteJavaexploredHome ;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

//Function to perform something when XmlHttpObject state changes
function pasteJavaexploredHome(){ 
 if ((xmlHttp.readyState==4 || xmlHttp.readyState=="complete") && xmlHttp.status ==200){   
	  document.getElementById("main").innerHTML=xmlHttp.responseText;
  } 
} 

function authUser(uname, pwd){
  //TODO: encrypt pwd string to be sent over network;
  pwd = encrypt(pwd);
  xmlHttp=CreateXmlHttpObject();
  var str = "sample";
  if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request");
  	return;
  } 
  var url="http://localhost:8080/DreamScope/LoginServlet";
  url=url+"?qparam1="+uname+"&qparam2="+pwd;
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange=allowUser;
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xmlHttp.send("sessionId="+session);

}

//Function to perform something when XmlHttpObject state changes
function allowUser(){ 
  if ((xmlHttp.readyState==4 || xmlHttp.readyState=="complete") && xmlHttp.status ==200){   
	  document.getElementById("main").innerHTML=xmlHttp.responseText;
  } 
} 

/*Use better password encryption/decryption algorithm*/
function encrypt(theText) {
	output = new String;
	temp = new Array();
	textSize = theText.length;
	for (i = 0; i < textSize; i++) {
		rnd = 68;
		temp[i] = theText.charCodeAt(i) +rnd;
	}
	for (i = 0; i < textSize; i++) {
		output += String.fromCharCode(temp[i]);
	}
	return output;
}


